<?php
class InquiryController extends AppController {

	public $name = 'inquiry';

	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','dopost');

	}
	
	public function index() {
        $goods_id = intval($_GET['gid']);
        if(!$goods_id) {
	        trigger_error("非法请求！");
        }
		$query = array(
				'joins' => array(
				array('type'=>'left','table'=>'shopinfo','conditions'=>array('goods.shop_id' =>'shopinfo.id')),					
				),
				'fields' => array('goods.*','shopinfo.*'),
				'conditions' => array('goods.id' =>$goods_id),
			);	
	    $info = POP::exe('[@@:UM goods][@@:QU find:all]',$query);			

        if(!$info) { trigger_error("产品不存在!"); }//没有此商品
        if(isset($this->my->id)) {
			$query = array(
					'fields' => array('userinfo.*'),
					'conditions' => array('userinfo.user_id' => $this->my->id),
					);	
			$user_info = POP::exe('[@@:UM userinfo][@@:QU find:first]',$query);		
			$user_info = $user_info['userinfo'];
	        $user_info['user_email'] = $this->my->email;
        }
        $this->set('info',$info[0]);
        $this->set('user_info',$user_info);		
        $this->set('goods_id',$goods_id);		
	}
	
	public function dopost() {
	    $user_id= $this->my->id;
        $post['shop_id'] = intval(Request::get_args('shop_id'));
        $post['goods_id'] = intval(Request::get_args('goods_id'));
        $post['goods_name'] = Request::get_args('goods_name');
        $post['user_id'] = intval($user_id);
        $post['name'] = Request::get_args('user_truename');
        $post['email'] = Request::get_args('user_email');
        $post['mobile'] = Request::get_args('user_mobile');
        $post['telphone'] = Request::get_args('user_telphone');
        $post['title'] = Request::get_args('title');
        $post['content'] = Request::get_args('content');
        $post['add_time'] = date("Y-m-d H:i:s",time());
        $post['add_ip'] = $_SERVER['REMOTE_ADDR'];
        $shop_id=$post['shop_id'];
		$savedata['shopinquiry'] = $post;		
		POP::exe('[@@:UM shopinquiry][@@:QU save]',$savedata);
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['user'] = array('inquiry_num +'=>1);				
		$query['set'] = $safeData;	
		$query['conditions'] = array('user.id'=>$shop_id);		
		POP::exe('[@@:UM user][@@:QU save]',$query);
		$this->flash("询价成功！", Router::url('shop/productshop/'.$post['goods_id']));
	}
	
	public function askprice() {
	    $shop_id= $this->my->id;

		$conditions = array();
		$conditions['shopinquiry.user_id'] = $shop_id;
		$conditions['shopinquiry.shop_del_status'] = 1;		
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'shopinquiry',
					'fields' => array('shopinquiry.*'),
					'conditions' => $conditions,
					'order' => 'shopinquiry.add_time desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result = $this->fpage($tb); 	
		
    }	
	
	
}

?>